

import java.io.Serializable;
import java.rmi.AccessException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.Registry;

public class RemoteObject implements RemoteInterface, Serializable {
	transient private String id;
	transient protected NetworkEventListener cl;
	
	public RemoteObject(String id){
		this.id = id;
	}
	public RemoteObject(String id, NetworkEventListener cl){
		this(id);
		this.cl = cl;
	}
	public void print(String output) throws RemoteException {
		System.out.println(output);
	}

	@Override
	public void fetchMe(String me) throws RemoteException {
		NetworkEvent event = new ConnectionEvent(me);
		cl.event(event);
	}

	@Override
	public String myId() throws RemoteException {
		return id;
	}
	
	public static RemoteInterface lookupRIObject(String id, Registry registry) throws RemoteException {
		RemoteInterface remoteInterface = new RemoteObject("nil");
		try {
			remoteInterface = (RemoteInterface) registry.lookup(id);
			System.out.println("Remote Object Detected");
		} catch (AccessException e) {
			e.printStackTrace();
		} catch (RemoteException e) {
			e.printStackTrace();
		}  catch (NotBoundException e) {
			System.out.println("Could not find the remote object you were looking for");
			e.printStackTrace();
		}
		return remoteInterface;
	}
	
}
